// PagedGui.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <mmsystem.h>
#include <sys/system.h>
#include <gal/ptgui.h>
#include <pages/pagectl.h>
#include <ial/input.h>
#include <widgets/object.h>
#include <crtdbg.h>

#define MAX_LOADSTRING 100

struct {
	const char *szWindowClass;
	const char *szWindowTitle;
	HINSTANCE hInst;								// current instance
	HWND hWnd;
	WNDPROC SDLProc;
} g_user_data = {"PTGUI_Simulate", "PTGUI Simulate", 0, 0};

HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void CALLBACK TickISR(unsigned int a,unsigned int b,unsigned long c,unsigned long d,unsigned long e);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	pt_event_t pt_msg;
	HACCEL hAccelTable;
	pt_uint8_t haveMsg;


	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_PAGEDGUI, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, SW_HIDE)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PAGEDGUI);

	pt_message_system_init();
	pt_gui_system_init(&g_user_data);	
	pt_input_system_init(g_user_data.hWnd);

	timeSetEvent(4, 0, TickISR, 0, TIME_PERIODIC);

	do
	{
		haveMsg = 0;
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			
			haveMsg = 1;
			if (msg.message == WM_QUIT)	
				goto exit;
			pt_input_system_update();

		}
		if (haveMsg == 0) {
			Sleep(100);
		}

		if ((pt_peek_message(&pt_msg) == PT_EOK))
		{					
			pt_translate_message(&pt_msg);
			pt_dispatch_message(&pt_msg);
		}
		else {
			ptgui_page_idle_handler();
		}
	} while (1);

exit:
	ptgui_page_system_free();
	ptgui_type_system_free();
	return msg.wParam;
}

void CALLBACK TickISR(unsigned int a,unsigned int b,unsigned long c,unsigned long d,unsigned long e)
{
	pt_tick_ISR();
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_PAGEDGUI);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_PAGEDGUI;
	wcex.lpszClassName	= g_user_data.szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   g_user_data.hInst = hInstance; // Store instance handle in our global variable

   g_user_data.hWnd = CreateWindow(g_user_data.szWindowClass, g_user_data.szWindowTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!g_user_data.hWnd)
   {
      return FALSE;
   }

   ShowWindow(g_user_data.hWnd, nCmdShow);
   UpdateWindow(g_user_data.hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		default:
			if (g_user_data.SDLProc != NULL)
			{
				return(g_user_data.SDLProc(hWnd, message, wParam, lParam));
			}
			else {
				return(DefWindowProc(hWnd, message, wParam, lParam));
			}
   }
   return 0;
}

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}
